package cn.heycm.tenant.common.datasource;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源配置
 *
 * @author hey
 * @version 1.0
 * @since 2024/8/24 23:48
 */
@Configuration
public class DataSourceConfiguration {


    /**
     * 租户动态数据源
     *
     * @return dataSource
     */
    @Bean
    public DataSource dataSource() {
        return new TenantDataSource();
    }

    /**
     * 全局事务管理器，跨租户会失效
     *
     * @param dataSource 租户动态数据源
     * @return transactionManager
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
